package com.youxue.teachin.user.login.presenter;

import com.library.uiframework.mvp.BasePresenter;
import com.youxue.teachin.common.utils.DefaultSubscriber;
import com.youxue.teachin.common.utils.PreContent;
import com.youxue.teachin.common.utils.PreferencesUtils;
import com.youxue.teachin.common.vo.ToKenVO;
import com.youxue.teachin.common.vo.UserVO;
import com.youxue.teachin.set.usercase.SetUserCase;
import com.youxue.teachin.user.login.ILoginView;
import com.youxue.teachin.user.login.usercase.LoginUserCase;
import com.youxue.teachin.user.userSystem.UserSystem;
import rx.Observable;
import rx.functions.Func1;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-05 16:31
 * @version: 9.1.0
 */
public class LoginPresenter extends BasePresenter<ILoginView> {
    private final LoginUserCase mLoginUserCase = new LoginUserCase();
    /**
     * 构造方法
     *
     * @param iLoginView 绑定的视图对象
     */
    public LoginPresenter(ILoginView iLoginView) {
        super(iLoginView);
    }

    public void login(String phone, String pasword) {
        Observable<ToKenVO> observable = mLoginUserCase.login(phone, pasword)
            .flatMap(new Func1<ToKenVO, Observable<ToKenVO>>() {
                @Override
                public Observable<ToKenVO> call(final ToKenVO toKenVO) {
                    PreferencesUtils.saveString(PreContent.LOGIN_THREAD, "phone");
                    return new SetUserCase()
                        .getUserInfo()
                        .map(new Func1<UserVO, ToKenVO>() {
                            @Override
                            public ToKenVO call(UserVO userVO) {
                                return toKenVO;
                            }
                        });
                }
            });

        execute(observable, new LoginSubscriber());
    }

    public void loginThreed(String code, String state) {
        Observable<ToKenVO> observable = mLoginUserCase.loginThreed(code, state)
            .flatMap(new Func1<ToKenVO, Observable<ToKenVO>>() {
                @Override
                public Observable<ToKenVO> call(final ToKenVO toKenVO) {
                    PreferencesUtils.saveString(PreContent.LOGIN_THREAD, "weixing");
                    return new SetUserCase()
                        .getUserInfo()
                        .map(new Func1<UserVO, ToKenVO>() {
                            @Override
                            public ToKenVO call(UserVO userVO) {
                                return toKenVO;
                            }
                        });
                }
            });

        execute(observable, new LoginSubscriber());
    }

    private class LoginSubscriber extends DefaultSubscriber<ToKenVO> {
        @Override
        public void onError(Throwable e) {
            super.onError(e);
            mBindView.loginFail();
            UserSystem.getInstance().clearToken();
        }

        @Override
        public void onNext(ToKenVO toKenVO) {
            super.onNext(toKenVO);
            mBindView.loginSuccess();
        }
    };
}
